package me.ywy.apt.msgimport.detail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import me.ywy.apt.msgimport.R;
import me.ywy.apt.msgimport.bean.MsgEntity;

public class DetailActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String MSG_KEY = "msg_key";
    public static final String POSITION_KEY = "position_key";
    public static final int DETAIL_REQUESTCODE = 1001;
    public static final int DETAIL_RESULTCODE = 1002;
    private EditText detailName;
    private EditText detailPhone;
    private EditText detailAddress;
    private EditText detailDesc;
    private Button detailReBtn;
    private Button tedailSubmitBtn;
    private MsgEntity sourceEntity;
    private int position;

    public static void startActivityForResult(AppCompatActivity activity, MsgEntity entity , int position) {
        Intent intent = new Intent(activity, DetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MSG_KEY, entity);
        bundle.putInt(POSITION_KEY, position);
        intent.putExtras(bundle);
        activity.startActivityForResult(intent, DETAIL_REQUESTCODE);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        initView();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        sourceEntity = (MsgEntity) bundle.getSerializable(MSG_KEY);
        position = bundle.getInt(POSITION_KEY);
        refreashView();
    }

    private void refreashView() {
        MsgEntity showEvent = sourceEntity.clone();

        detailName.setText(showEvent.getName());
        detailPhone.setText(showEvent.getPhone());
        detailAddress.setText(showEvent.getAddress());
        detailDesc.setText(showEvent.getDesc());
    }


    private void initView() {
        detailName = (EditText) findViewById(R.id.detail_name);
        detailPhone = (EditText) findViewById(R.id.detail_phone);
        detailAddress = (EditText) findViewById(R.id.detail_address);
        detailDesc = (EditText) findViewById(R.id.detail_desc);
        detailReBtn = (Button) findViewById(R.id.detail_re_btn);
        tedailSubmitBtn = (Button) findViewById(R.id.tedail_submit_btn);

        detailReBtn.setOnClickListener(this);
        tedailSubmitBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_re_btn:
                refreashView();
                break;
            case R.id.tedail_submit_btn:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String name = detailName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String phone = detailPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String address = detailAddress.getText().toString().trim();
        if (TextUtils.isEmpty(address)) {
            Toast.makeText(this, "地址不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String desc = detailDesc.getText().toString().trim();
        if (TextUtils.isEmpty(desc)) {
            Toast.makeText(this, "描述不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        MsgEntity returnMsg = new MsgEntity();
        returnMsg.setName(name);
        returnMsg.setAddress(address);
        returnMsg.setPhone(phone);
        returnMsg.setDesc(desc);
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable(MSG_KEY,returnMsg);
        bundle.putInt(POSITION_KEY,position);
        intent.putExtras(bundle);
        setResult(DETAIL_RESULTCODE , intent);
        finish();

    }
}
